package zucc.lcj.android_finalwork;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class PayActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    EditText user_pwd;
    Button payBtn;
    private int integral;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);

        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);

        payBtn = (Button) findViewById(R.id.payBtn);
        user_pwd = (EditText) findViewById(R.id.user_pwd);

        payBtn.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                boolean isHas = false;
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String password = String.valueOf(user_pwd.getText());
                Cursor cursor = db.rawQuery("select * from UserInfo where user_id=?",new String[]{String.valueOf(LoginActivity.global_login_user_id)});
                cursor.moveToFirst();
                integral = cursor.getInt(cursor.getColumnIndex("integral"));
                if(cursor.moveToFirst()==false)
                    Toast.makeText(getApplicationContext(), "表中没有数据", Toast.LENGTH_SHORT).show();
                else {
                    if (cursor.moveToFirst()) {
                        if (cursor.getString(cursor.getColumnIndex("user_pwd")).equals(password)) {
                            isHas = true;
                        }
                    }
                    cursor.close();
                    if (isHas == true) {
                        if(integral>=2){
                            String stillhave=String.valueOf(integral-2);
                            db.execSQL("update UserInfo set integral=?",new String[]{stillhave});

//                            SQLiteDatabase db = dbHelper.getWritableDatabase();
                            String name = LoginActivity.global_login_user;
                                if (FriendsActivity.sexs == 1) {
                                    int countMen=0;
                                    String wxMen="";
                                    int flag=0;
                                    Cursor cursor1 = db.rawQuery("select * from WechatMen",null);
                                    if (cursor1.moveToFirst()){
                                        do{
                                            countMen++;
                                        }while (cursor1.moveToNext());
                                    }
                                    System.out.println("countMen"+countMen);
                                    if (countMen==0){
                                        Toast.makeText(getApplicationContext(), "池中没有男同志的微信", Toast.LENGTH_SHORT).show();
                                    }else {
                                        while (flag == 0) {
                                            int whichMen = (int) (Math.random() * countMen + 1);
                                            System.out.println("whichWomen" + whichMen);
                                            String sqlMen = "select * from WechatMen";
                                            Cursor cursor2 = db.rawQuery(sqlMen, null);
                                            for (int i = whichMen; i > 0; i--) {
                                                if (cursor2.moveToNext()) {
                                                    wxMen = cursor2.getString(cursor2.getColumnIndex("wechatMen_name"));
                                                    System.out.println("wxMen" + i);
                                                }
                                            }
                                            System.out.println("wx" + wxMen);
                                            System.out.println("global " + LoginActivity.global_login_user_wechat);
                                            System.out.println("mywx " + FriendsActivity.mywx);
                                            if (wxMen.equals(LoginActivity.global_login_user_wechat) && wxMen.equals(FriendsActivity.mywx)) {
                                                flag = 0;
                                            } else flag = 1;
                                            System.out.println("flag" + flag);
                                        }
                                        AlertDialog alertDialog2 = new AlertDialog.Builder(PayActivity.this)
                                                .setTitle("网络一线牵")
                                                .setMessage(wxMen)
                                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        Intent intent = new Intent(PayActivity.this, FriendsActivity.class);
                                                        startActivity(intent);
                                                    }//添加"Yes"按钮
                                                })
                                                .create();
                                        alertDialog2.show();
                                    }
                                }
                                else if(FriendsActivity.sexs==2){
//                                    SQLiteDatabase db = dbHelper.getWritableDatabase();
//                                    String name = LoginActivity.global_login_user;
                                    int countWomen=0;
                                    String wxWomen="";
                                    int flag1=0;
                                    Cursor cursor3 = db.rawQuery("select * from WechatWomen",null);
                                    if (cursor3.moveToFirst()){
                                        do{
                                            countWomen++;
                                        }while (cursor3.moveToNext());
                                    }
                                    System.out.println("countwomen"+countWomen);
                                    if (countWomen==0){
                                        Toast.makeText(getApplicationContext(), "池中没有女同志的微信", Toast.LENGTH_SHORT).show();
                                    }
                                    else {
                                        while (flag1==0) {
                                            int whichWomen = (int) (Math.random() * countWomen + 1);
                                            System.out.println("whichWomen" + whichWomen);
                                            String sqlWomen = "select * from WechatWomen";
                                            Cursor cursor4 = db.rawQuery(sqlWomen, null);
                                            for (int i = whichWomen; i > 0; i--) {
                                                if (cursor4.moveToNext()) {
                                                    wxWomen = cursor4.getString(cursor4.getColumnIndex("wechatWomen_name"));
                                                    System.out.println("wxWomen" + i);
                                                }
                                            }
                                            System.out.println("wx" + wxWomen);
                                            System.out.println("global " + LoginActivity.global_login_user_wechat);
                                            System.out.println("mywx " + FriendsActivity.mywx);
                                            if (wxWomen.equals(LoginActivity.global_login_user_wechat)&&wxWomen.equals(FriendsActivity.mywx)) {
                                                flag1 = 0;
                                            }
                                            else flag1=1;
//                                            System.out.println("flag1" + );
                                        }

                                        AlertDialog alertDialog2 = new AlertDialog.Builder(PayActivity.this)
                                                .setTitle("网络一线牵")
                                                .setMessage(wxWomen)
                                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        Intent intent = new Intent(PayActivity.this, FriendsActivity.class);
                                                        startActivity(intent);
                                                    }//添加"Yes"按钮
                                                })
                                                .create();
                                        alertDialog2.show();
                                    }
                                }
                        }else{
                            Toast.makeText(PayActivity.this, "余额不足", Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        Toast.makeText(getApplicationContext(), "密码错误，请重试", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}
